import 'package:flutter/material.dart';
import 'package:learning_content_admin_pannel.core/core/presentation/theme/styles.dart';

import 'text_theme.dart';

class CoreTheme extends InheritedTheme {
  CoreTheme({
    required Brightness brightness,
    required Color primaryColor,
    required Color primaryContrastingColor,
    required super.child,
    super.key,
  })  : _brightness = brightness,
        _primaryColor = primaryColor,
        _primaryContrastingColor = primaryContrastingColor,
        _data = _buildThemeData(
          brightness: brightness,
          primaryColor: primaryColor,
          primaryContrastingColor: primaryContrastingColor,
        );

  final Brightness _brightness;
  final Color _primaryColor;
  final Color _primaryContrastingColor;
  final CoreThemeData _data;

  static CoreThemeData _buildThemeData({
    required Brightness brightness,
    required Color primaryColor,
    required Color primaryContrastingColor,
  }) {
    return switch (brightness) {
      _ => CoreThemeData.dark(
        primaryColor: primaryColor,
        primaryContrastingColor: primaryContrastingColor,
      ),
    };
  }

  static CoreThemeData of(BuildContext context) {
    final inheritedTheme = context.dependOnInheritedWidgetOfExactType<CoreTheme>()!;
    return inheritedTheme._data;
  }

  @override
  Widget wrap(BuildContext context, Widget child) {
    final ancestorTheme = context.findAncestorWidgetOfExactType<CoreTheme>();

    if (identical(this, ancestorTheme)) {
      return child;
    }

    return CoreTheme(
      brightness: _brightness,
      primaryColor: _primaryColor,
      primaryContrastingColor: _primaryContrastingColor,
      child: child,
    );
  }

  @override
  bool updateShouldNotify(CoreTheme oldWidget) {
    return _brightness != oldWidget._brightness ||
        _primaryColor != oldWidget._primaryColor ||
        _primaryContrastingColor != oldWidget._primaryContrastingColor;
  }
}

class CoreThemeData {
  const CoreThemeData._internal({
    required this.brightness,
    required this.primaryColor,
    required this.primaryContrastingColor,
    required this.backgroundColor,
    required this.headerBackgroundColor,
    required this.headerDividerColor,
    required this.textTheme,
  });

  factory CoreThemeData.dark({
    required Color primaryColor,
    required Color primaryContrastingColor,
  }) {
    return CoreThemeData._internal(
      brightness: Brightness.dark,
      primaryColor: primaryColor,
      primaryContrastingColor: primaryContrastingColor,
      backgroundColor: CoreStyles.darkGrey5,
      headerBackgroundColor: CoreStyles.darkGrey3,
      headerDividerColor: Colors.black,
      textTheme: CoreTextTheme.dark(),
    );
  }

  final Brightness brightness;
  final Color primaryColor;
  final Color primaryContrastingColor;
  final Color backgroundColor;
  final Color headerBackgroundColor;
  final Color headerDividerColor;

  final CoreTextTheme textTheme;
}
